package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;

import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class NursingPlanServiceImpl implements NursingPlanService {



    @Autowired
    private  NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    @Override  // 3.1 查询所有护理计划
    public List<NursingPlanVo> selectAll() {
        return nursingPlanMapper.selectAll();
    }

    @Override // 3.2 新增护理计划（包括护理项目，连NursingProjectPlan表。在查询NursingProject表）
    @Transactional(rollbackFor = {Exception.class})
    public void insertNursingPlan(NursingPlanDto nursingPlanDto) {
            NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
            // 校验新增护理计划重名
        String planName = nursingPlan.getPlanName();
     int count =  nursingPlanMapper.CountByPlanName(planName);
     if(count>0){
         throw new  BaseException(BasicEnum.NURSING_PLAN_INSERT_FAIL_BY_NAME);
     }
     // 没有重名护理计划，可以进行新增操作
        nursingPlan.setStatus(1);
        nursingPlanMapper.insert(nursingPlan);
        Long nursingPlanId = nursingPlan.getId();
        List<NursingProjectPlanVo> projectPlanVoList = nursingPlan.getProjectPlans();
        // 护理计划中的 护理项目集合不为空，给每一个护理项目绑定id
        if(projectPlanVoList !=null && !projectPlanVoList.isEmpty() ) {
            for (NursingProjectPlanVo nursingProjectPlanVo : projectPlanVoList) {
                nursingProjectPlanVo.setPlanId(nursingPlanId);
            }
            // 批量添加护理项目
            nursingProjectPlanService.insertBatch(projectPlanVoList);
        }
    }

    // 3.3 添加分页查询护理计划
    @Override
    public PageResponse<NursingPlanVo> listByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlan> nursingPlans = nursingPlanMapper.listByPage(name,status);
        return PageResponse.of(nursingPlans,NursingPlanVo.class);
    }

    //  3.4 根据id查询护理计划
    @Override
    public NursingPlanVo getById(Long id) {
        // 校验id是否存在
       NursingPlanVo nursingPlanVo = nursingPlanMapper.getNPById(id);
       if(nursingPlanVo==null){
           throw new BaseException(BasicEnum.NURSING_PLAN_QUERY_FAIL_BY_ID);
       }
        return nursingPlanVo;
    }

    // 3.5 修改护理计划
    @Transactional
    @Override
    public void updateNP(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        // 删除原来的所有护理计划和项目关联表中的Ids
        NursingPlanVo nursingPlanVoById = nursingPlanMapper.getNPById(nursingPlanDto.getId());
        List<Long> ids = nursingPlanVoById.getProjectPlans().stream().map(NursingProjectPlanVo::getId).collect(Collectors.toList());
        nursingProjectPlanService.deleteByIds(ids);
        if (CollUtil.isNotEmpty(nursingPlan.getProjectPlans())) {
            nursingPlan.getProjectPlans().forEach(v -> v.setPlanId(nursingPlan.getId()));
            nursingProjectPlanService.insertBatch(nursingPlan.getProjectPlans());
        }
        nursingPlanMapper.updateNP(nursingPlan);
    }



    //  3.6 删除护理计划
    @Override
    public void deleteNPById(Long nursingPlanId) {
        // 删除护理计划
        // 校验id是否存在
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getNPById(nursingPlanId);
        if(nursingPlanVo ==null){
            throw new BaseException(BasicEnum.NURSING_PLAN_QUERY_FAIL_BY_ID);
        }
        // 判断护理计划是否是禁用状态，才能删除
        if(nursingPlanVo.getStatus()==1){
            throw new BaseException(BasicEnum.NURSING_PLAN_DELETE_FAIL_BY_STATUS);
        }
        nursingPlanMapper.deleteNPById(nursingPlanId);
        // 删除护理计划下面的护理项目
        nursingProjectPlanService.deleteByNursingPlanId(nursingPlanId);

    }

    // 3.7 启用/禁用护理计划
    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }


}
